package dm.repository;

import dm.ast.DmDataType;
import test.envset.GlobalState;
import test.log.SqlLog;
import test.utility.SqlRun;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 元数据仓库
 * dm内部函数的元信息
 */
public final class DmIFunInfo {
    public static ArrayList<ArrayList<IFunction>> dmIFuns = new ArrayList<ArrayList<IFunction>>();
    public static boolean initFlag = false;
    public static String getIFunSql = "select a.id,a.name,b.seq,data_type,len,io_type"
            + "from v$ifun a , v$ifun_arg b "
            + "where a.id = b.id and "
            + "data_type not in ('UNKNOW DATATYPE', 'CURSOR' , 'BFILE' , '<DMAOT>') "
            + "order by a.id , b.id , b.seq;";

    //初始化Dm内部函数
    public synchronized static void initDmIFunInfo(SqlRun sqlr){
        dmIFuns.clear();
        int retype_cnt = DmDataType.values().length;

        for (int i = 0; i < retype_cnt; i++) {
            dmIFuns.add(new ArrayList<IFunction>());
        }

        ResultSet rs = sqlr.sqlWithRsBegin(getIFunSql);

        if(rs == null){
            System.out.println(getIFunSql);
            System.out.println("--init DmIFunInfo Failed!");
            System.out.println(sqlr.getErrMsg());
            return;
        }

        try {
            int idused = -1;
            int id = -1;
            String fname = "";
            int paraSeq = -2;
            IFunction tmpFun = null;
            int cnt=0;
            while(rs.next()){
                id = rs.getInt(1);
                if(rs.getInt(3) == -1){   // seq值为-1，表示一个函数的参数列表开始
                    if(tmpFun != null){
                        dmIFuns.get(tmpFun.reType.getIndex()).add(tmpFun);
//                        cnt++;
                        tmpFun = null;
                    }
                    fname = rs.getString(2);
                    tmpFun = new IFunction(id , fname , DmDataType.getDmDataType(rs.getString(4)));
                    idused = id;
                }else {
                    tmpFun.addPara(DmDataType.getDmDataType(rs.getString(4)));
                }
            }
            initFlag = true;
        } catch (SQLException e) {
            System.out.println("init DmIFunInfo Failed!");
            System.out.println(e.getMessage());
        } finally {
            sqlr.sqlWithRsFinish();
        }
    }


    public static void outputIfuns(){
        for(ArrayList<IFunction> ifuns : dmIFuns){
            for(IFunction tmp : ifuns){
                System.out.println(tmp.getInfo());
            }
        }
    }

    //测试所有函数
    public static void testIfuns(GlobalState global){
        if(!initFlag){
            System.out.println("init DmIFunInfo Failed , can't testIfuns");
        }
        SqlLog sqlLog = global.getSqlLog();
        for(ArrayList<IFunction> ifuns : dmIFuns){
            for(IFunction tmp : ifuns){
                String sql = tmp.getTestSql();
                boolean status = global.executeSelect(sql);
                if(!status){
                    tmp.setRunableFlag(false);
                }else{
                    sqlLog.sqlLogMessage(sql + SqlLog.SQL_END);
                }

            }
        }
    }
}
